/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Eventos;

import InterfaceEI.JanelaPrincipal;
import InterfaceEI.PanelPrincipal;
import Utils.Utilidades;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.peer.PanelPeer;
import javax.swing.JOptionPane;

/**
 *
 * @author Isabela
 */
public class MouseEventosDistribuida implements MouseMotionListener, MouseListener{
    private PanelPrincipal panelP;
    private JanelaPrincipal frame;

    private boolean fim = false;
    private boolean desenhou = false;

    private double forca;
    Utilidades utili = new Utilidades();

    public MouseEventosDistribuida(PanelPrincipal panelP, JanelaPrincipal frame) {
        this.panelP = panelP;
        this.frame = frame;
        this.forca = forca;
    }



    public void mouseDragged(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
        if(!desenhou){
            if(!fim){
               panelP.setxInicioDist(e.getX());

               panelP.repaint();
               panelP.validate();
            }
            else{
               panelP.setxFimDist(e.getX());

               panelP.repaint();
               panelP.validate();
            }
        }
        else{
               if ((e.getX() >= panelP.getxInicio()) && (e.getX() <= panelP.getxFim())) {
                if ((e.getY() >= panelP.getyInicio() - 5) && (e.getY() <= panelP.getyInicio() + 5)) {
                    if (frame.getExibeMomento().isSelected()) {
                        double x = utili.conversaoPixelMX(e.getX() - panelP.getxInicio(), panelP.getWidth());
                        double momento = frame.getDiagramas().MomentoFletorNoPonto(x);
                        panelP.setMomentoFletor(true);
                        panelP.setxMomento(e.getX());
                        panelP.setyMomento(e.getY());
                        panelP.setValorMomento(momento);

                        panelP.repaint();
                        panelP.validate();
                    }
                    if (frame.getExibeEsforco().isSelected()) {
                        double x = utili.conversaoPixelMX(e.getX() - panelP.getxInicio(), panelP.getWidth());
                        double esforco = frame.getDiagramas().EsforcoCortanteNoPonto(x);
                        panelP.setEsforcoCortante(true);
                        panelP.setxCortante(e.getX());
                        panelP.setyCortante(e.getY());
                        panelP.setValorCortante(esforco);

                        panelP.repaint();
                        panelP.validate();
                    }
                    if (frame.getExibeDeformada().isSelected()) {
                        double x = utili.conversaoPixelMX(e.getX() - panelP.getxInicio(), panelP.getWidth());
                        double deformada = frame.getDiagramas().DeformadaNoPonto(x);
                        panelP.setDeformadaValor(true);
                        panelP.setxDeformada(e.getX());
                        panelP.setyDeformada(e.getY());
                        panelP.setValorDeformada(deformada);

                        panelP.repaint();
                        panelP.validate();
                    }
                }
            }
            else {
                panelP.setMomentoFletor(false);
                panelP.setEsforcoCortante(false);
                panelP.setDeformadaValor(false);

                panelP.repaint();
                panelP.validate();
            }
        }
    }

    public void mouseClicked(MouseEvent e) {
        panelP.setMomentoFletor(false);
        if (!desenhou) {
            if (!fim) {
                if ((e.getX() >= panelP.getxInicio()) && (e.getX() <= panelP.getxFim())) {
                    panelP.setxInicioDist(e.getX());
                    panelP.setDistribuida(true);
                    panelP.setDesenhouD(false);

                    panelP.repaint();
                    panelP.validate();

                    fim = true;
                }
                else{
                    JOptionPane.showMessageDialog(null, "A carga está fora da viga!");
                }
            }
            else {
                if ((e.getX() <= panelP.getxFim()) && (e.getX() >= panelP.getxInicio())) {
                    panelP.setxFimDist(e.getX());
                    panelP.setDistribuida(true);
                    panelP.setDesenhouD(true);

                    String t = new String();
                    //t= "0.0";
                    if(frame.getForcaD() != 0){
                        //t = JOptionPane.showInputDialog(null, "Entre com a intensidade da carga (N):", "Carga Concentrada", JOptionPane.QUESTION_MESSAGE);
                    }
                    else {
                         boolean auxB = false;
                        do {
                            do {
                                t = JOptionPane.showInputDialog(null, "Entre com a intensidade da carga (N):", "Carga Concentrada", JOptionPane.QUESTION_MESSAGE);
                            } while (t == null || t.equalsIgnoreCase(""));
                            //atribuindo o valor da força na classe principal (Frame)
                            try {
                                forca = Double.parseDouble(t);
                                frame.setForcaD(forca);
                                auxB = false;
                            }
                            catch (NumberFormatException ex) {
                                auxB = true;
                            }
                            
                        } while (auxB);
                    }

                    
                    int tam;
                    int xI;
                    if (panelP.getxInicioDist() < panelP.getxFimDist()) {
                        xI = panelP.getxInicioDist();
                        tam = panelP.getxFimDist() - xI;
                    }
                    else {
                        xI = panelP.getxFimDist();
                        tam = panelP.getxInicioDist() - xI;
                    }

                    //atribuindo a coordenada x do inicio da carga distribuida na classe principal (Frame)
                    double x = utili.conversaoPixelMX(xI - panelP.getxInicio(), panelP.getWidth());
                    frame.setxDistribuida(x);

                    //atribuindo o valor do tamanho da carga distribuida na classe principal (Frame)
                    double tamanho = utili.conversaoPixelMX(tam, panelP.getWidth());
                    frame.setTamanhoD(tamanho);

                    frame.setTextForcaDist();
                    frame.setTextTamDist();
                    frame.setTextXDist();

                    if (frame.getEnableCarga()) {
                        frame.calcula();
                        frame.converteGrafico();

                        if (frame.getExibeGraficoEC().isSelected()) {
                            panelP.setCortante(frame.getPontosCortante());

                            panelP.setGraficoCortante(true);

                            panelP.repaint();
                            panelP.validate();
                        }
                        if (frame.getExibeGraficoMF().isSelected()) {
                            panelP.setFletor(frame.getPontosFletor());

                            panelP.setGraficoFletor(true);

                            panelP.repaint();
                            panelP.validate();
                        }
                        if (frame.getExibeGraficoD().isSelected()) {
                            panelP.setDeformada(frame.getPontosDeformada());

                            panelP.setGraficoDeformada(true);

                            panelP.repaint();
                            panelP.validate();
                        }

                        if (panelP.getFixoD() || panelP.getDesliF()) {
                            panelP.setReacaoBiapoiadaA(true);
                            panelP.setReacaoBiapoiadaB(true);

                            panelP.setBiapoiadaA(frame.getDiagramas().getReacaoBiemA());
                            panelP.setBiapoiadaB(frame.getDiagramas().getReacaoBiemB());
                        }
                        else{
                             panelP.setReacaoBiapoiadaA(false);
                             panelP.setReacaoBiapoiadaB(false);

                            panelP.setReacaoEngastada(true);
                            panelP.setValorReacaoE(frame.getDiagramas().getReacaoApoioEngastado());
                        }


                    }

                    panelP.addMouseListener(frame.getMouseMomento());
                    panelP.addMouseMotionListener(frame.getMouseMomento());

                    panelP.addMouseListener(frame.getMouseConcentrada());
                    panelP.addMouseMotionListener(frame.getMouseConcentrada());

                    panelP.repaint();
                    panelP.validate();

                    fim = false;
                    desenhou = true;
                }
                else{
                    JOptionPane.showMessageDialog(null, "A carga está fora da viga!");
                }
            }
        }
        else{
            int xI = 0;
            int xF = 0;
            if(panelP.getxInicioDist() < panelP.getxFimDist()){
                xI = panelP.getxInicioDist();
                xF = panelP.getxFimDist();
            }
            else{
                xI = panelP.getxFimDist();
                xF = panelP.getxInicioDist();
            }
             if((e.getX() >= xI - 5) && (e.getX() <= xF + 5)){
                 if((e.getY() >= panelP.getyInicio() - 35) && (e.getY() <= panelP.getyInicio() + 5)){
                     if ((e.getX() < panelP.getxConcentrada() - 10) || (e.getX() > panelP.getxConcentrada() + 10)) {
                         if ((e.getX() < panelP.getxCMomento() - 20) || (e.getX() > panelP.getxCMomento() + 35)) {
                             desenhou = false;
                             fim = false;
                             panelP.setDesenhouD(false);

                             panelP.setxInicioDist(0);
                             panelP.setxFimDist(0);

                             panelP.removeMouseListener(frame.getMouseMomento());
                             panelP.removeMouseMotionListener(frame.getMouseMomento());

                             panelP.removeMouseListener(frame.getMouseConcentrada());
                             panelP.removeMouseMotionListener(frame.getMouseConcentrada());

                             panelP.repaint();
                             panelP.validate();
                         }
                     }
                 }
             }
        }
        panelP.repaint();
        panelP.validate();
    }

    public void mousePressed(MouseEvent e) {

    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }

    public void setPanelP(PanelPrincipal panelP) {
        this.panelP = panelP;
    }

    public void setFrame(JanelaPrincipal frame) {
        this.frame = frame;
    }

    

}
